using WpfClient.Core.Models;

namespace WpfClient.Core.Communication
{
    /// <summary>
    /// Socket通信服务接口
    /// </summary>
    public interface ISocketCommunicationService
    {
        /// <summary>
        /// 服务是否正在运行
        /// </summary>
        bool IsRunning { get; }

        /// <summary>
        /// 当前连接的客户端数量
        /// </summary>
        int ConnectedClientsCount { get; }

        /// <summary>
        /// 客户端连接事件
        /// </summary>
        event EventHandler<ClientConnectedEventArgs> ClientConnected;

        /// <summary>
        /// 客户端断开连接事件
        /// </summary>
        event EventHandler<ClientDisconnectedEventArgs> ClientDisconnected;

        /// <summary>
        /// 通信状态变化事件
        /// </summary>
        event EventHandler<CommunicationStatusEventArgs> StatusChanged;

        /// <summary>
        /// 启动通信服务
        /// </summary>
        /// <param name="port">监听端口</param>
        Task StartAsync(int port);

        /// <summary>
        /// 停止通信服务
        /// </summary>
        Task StopAsync();

        /// <summary>
        /// 添加数据到发送队列
        /// </summary>
        /// <param name="clientId">客户端ID</param>
        /// <param name="data">要发送的数据</param>
        Task QueueDataAsync(string clientId, byte[] data);

        /// <summary>
        /// 向所有客户端发送数据
        /// </summary>
        /// <param name="data">要发送的数据</param>
        Task QueueDataToAllAsync(byte[] data);

        /// <summary>
        /// 获取连接的客户端列表
        /// </summary>
        Task<IEnumerable<ClientInfo>> GetConnectedClientsAsync();

        /// <summary>
        /// 创建标准协议数据包
        /// </summary>
        /// <param name="command">命令类型</param>
        /// <param name="subCommand">子命令</param>
        /// <param name="payload">负载数据</param>
        byte[] CreateProtocolPacket(byte command, byte subCommand, byte[]? payload = null);
    }

    /// <summary>
    /// 通信状态事件参数
    /// </summary>
    public class CommunicationStatusEventArgs : EventArgs
    {
        public string Status { get; }
        public string? Details { get; }

        public CommunicationStatusEventArgs(string status, string? details = null)
        {
            Status = status;
            Details = details;
        }
    }

    /// <summary>
    /// 协议命令定义
    /// </summary>
    public static class ProtocolCommands
    {
        // 发送命令
        public const byte COMMAND_02 = 0x02;
        public const byte COMMAND_03 = 0x03;
        public const byte SUB_COMMAND_01 = 0x01;

        // 回复命令
        public const byte RESPONSE_20 = 0x20;
        public const byte RESPONSE_30 = 0x30;
        public const byte RESPONSE_SUB_10 = 0x10;

        // 协议头
        public static readonly byte[] PROTOCOL_HEADER = { 0x55, 0xAA };

        // 创建发送数据包
        public static byte[] CreateSendPacket_02_01(byte[]? payload = null)
        {
            return CreatePacket(COMMAND_02, SUB_COMMAND_01, payload);
        }

        public static byte[] CreateSendPacket_03_01(byte[]? payload = null)
        {
            return CreatePacket(COMMAND_03, SUB_COMMAND_01, payload);
        }

        // 创建期望的回复头
        public static byte[] CreateExpectedResponse_20_10()
        {
            return new byte[] { 0x55, 0xAA, RESPONSE_20, RESPONSE_SUB_10 };
        }

        public static byte[] CreateExpectedResponse_30_10()
        {
            return new byte[] { 0x55, 0xAA, RESPONSE_30, RESPONSE_SUB_10 };
        }

        private static byte[] CreatePacket(byte command, byte subCommand, byte[]? payload)
        {
            var packet = new List<byte>();
            packet.AddRange(PROTOCOL_HEADER);
            packet.Add(command);
            packet.Add(subCommand);
            
            if (payload != null && payload.Length > 0)
            {
                packet.AddRange(payload);
            }

            return packet.ToArray();
        }
    }
}